//
//  ZYCTeaAttendanceClockInView.m
//  ZDSC
//
//  Created by 张威 on 2021/10/22.
//

#import "ZYCTeaAttendanceClockInView.h"
#import <FSCalendar.h>
#import "ZYCTeaLeaveRecordModel.h"
#import "ZYCTeaAttendanceClockInStatisticsView.h"
#import "UIButton+EnlargeEdge.h"
#import "LunarFormatter.h"

@interface ZYCTeaAttendanceClockInView ()<FSCalendarDelegate,FSCalendarDataSource>

@property (nonatomic, strong) FSCalendar *calendar;
@property (nonatomic, strong) LunarFormatter *lunarFormatter;
@property (nonatomic, strong) NSCalendar *gregorian;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *calendaerImgView;
@property (nonatomic, strong) UIButton *previousButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end
@implementation ZYCTeaAttendanceClockInView

- (void)reloadCalendarData{
    [self.calendar reloadData];
}

- (void)setModel:(ZYCTeaLeaveRecordModel *)model{
    _model = model;
    self.staticsBottomView.model = model;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"#FAFAFA");
        [self setupContaints];
        self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        self.lunarFormatter = [[LunarFormatter alloc] init];
    }
    return self;
}


#pragma mark - FSCalendarDataSource && FSCalendarDelegate
- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date{
    if ([self.gregorian isDateInToday:date]) {
        return @"今";
    }
    return nil;
}
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
    return [self.lunarFormatter stringFromDate:date];
}
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date{
    NSString *dateString = [self.dateFormatter stringFromDate:date];
    if ([self.normalArr containsObject:dateString]) {
        return 1;
    }
    if ([self.exceptArr containsObject:dateString]) {
        return 1;
    }else{
        return 0;
    }
}

- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance fillDefaultColorForDate:(NSDate *)date{
    NSString *dateString = [self.dateFormatter stringFromDate:date];
    if ([self.normalArr containsObject:dateString]) {
        return HEX_RGB(@"#219D51");
    }else if ([self.exceptArr containsObject:dateString]) {
        return HEX_RGB(@"#E93C06");
    }else{
        return nil;
    }
}

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    NSString *timeStr = [self.dateFormatter stringFromDate:date];
    if ([self.delegate respondsToSelector:@selector(teaAttendanceClockInViewDidSelectDate:)]) {
        [self.delegate teaAttendanceClockInViewDidSelectDate:timeStr];
    }
}

// 切换月份
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
    NSString *dateString = [self.dateFormatter stringFromDate:calendar.currentPage];
    if ([self.delegate respondsToSelector:@selector(teaAttendanceClockInViewDidSelectDate:)]) {
        [self.delegate teaAttendanceClockInViewDidSelectDate:dateString];
    }
}


#pragma mark - action method
-(void)previousClicked:(UIButton *)sender{
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *previousMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:previousMonth animated:YES];
}

-(void)nextClicked:(UIButton *)sender{
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:nextMonth animated:YES];
}




#pragma mark - getters
- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

- (NSArray *)normalArr {
    if (!_normalArr) {
        _normalArr = [[NSArray alloc] init];
    }
    return _normalArr;
}

- (NSArray *)exceptArr {
    if (!_exceptArr) {
        _exceptArr = [[NSArray alloc] init];
    }
    return _exceptArr;
}

- (NSArray *)workOvertimeArr {
    if (!_workOvertimeArr) {
        _workOvertimeArr = [[NSArray alloc] init];
    }
    return _workOvertimeArr;
}


- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ty"]];
    }
    return _bgImageView;
}

- (UIImageView *)calendaerImgView {
    if (!_calendaerImgView) {
        _calendaerImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rili"]];
    }
    return _calendaerImgView;
}


- (FSCalendar *)calendar {
    if (!_calendar) {
        _calendar = [[FSCalendar alloc] init];
        _calendar.delegate = self;
        _calendar.dataSource = self;
        _calendar.headerHeight = 50.0f;
        _calendar.appearance.headerMinimumDissolvedAlpha = 0;
        _calendar.locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
        _calendar.backgroundColor = [UIColor whiteColor];
        _calendar.placeholderType = FSCalendarPlaceholderTypeNone; //只显示当前月份
        _calendar.appearance.headerDateFormat = @"yyyy年MM月";// 设置头部时间格式
        _calendar.appearance.weekdayTextColor = HEX_RGB(@"#62676A");//设置周字体颜色
        _calendar.appearance.headerTitleColor = HEX_RGB(@"#262A28");//设置头字体颜色
        _calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
    }
    return _calendar;
}

- (UIButton *)previousButton {
    if (!_previousButton) {
        _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_previousButton setImage:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
        [_previousButton addTarget:self action:@selector(previousClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _previousButton;
}

- (UIButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setImage:[UIImage imageNamed:@"you"] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(nextClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}

- (ZYCTeaAttendanceClockInStatisticsView *)staticsBottomView {
    if (!_staticsBottomView) {
        _staticsBottomView = [[ZYCTeaAttendanceClockInStatisticsView alloc] init];
        _staticsBottomView.hidden = YES;
    }
    return _staticsBottomView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [UILabel LabelWithString:@"今日无考勤" Font:kCustomFont(@"PingFangSC-Regular", 14.0f) titleColor:HEX_RGB(@"999999") textAlignment:ZZTextAlignmentCenter];
        _tipLabel.hidden = YES;
    }
    return _tipLabel;
}
#pragma mark - UI
- (void)setupContaints{
    [self addSubview:self.bgImageView];
    [self insertSubview:self.bgImageView atIndex:0];
    [self addSubview:self.calendaerImgView];
    [self addSubview:self.calendar];
    [self.calendar addSubview:self.previousButton];
    [self.calendar addSubview:self.nextButton];
    [self addSubview:self.staticsBottomView];
    [self addSubview:self.tipLabel];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(138.0f);
    }];

    [self.calendaerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(27.0f);
    }];
    
    [self.calendar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10.0f);
        make.right.equalTo(self).offset(-10.0f);
        make.top.equalTo(self.calendaerImgView.mas_centerY);
        make.height.mas_equalTo(282);
    }];

    [self.previousButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.calendar).offset(95.0f);
        make.centerY.equalTo(self.calendar.mas_top).offset(27.0f);
        make.size.mas_equalTo(CGSizeMake(10, 8.5));
    }];

    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.calendar).offset(-95.0f);
        make.centerY.equalTo(self.calendar.mas_top).offset(27.0f);
        make.size.mas_equalTo(CGSizeMake(10, 8.5));
    }];


    [self.staticsBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.calendar.mas_bottom);
        make.height.mas_equalTo(250.0f);
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.calendar.mas_bottom).offset(162.0f);
    }];
    
    [self.previousButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
    [self.nextButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
}



@end
